 /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
<template>
  <el-row
    :gutter="20"
    style="margin-top:-20px;"
  >
    <el-col
      :lg="6"
      :sm="12"
      v-for="item in list"
      :key="item.id"
    >
      <div
        class="m-card"
        :class="{'is-home':item.node === 1,'is-center':item.central === 1}"
      >
        <div class="m-card__header">
          <span>{{ item.name }}</span>
          <div class="btn-group">
            <el-tooltip
              v-auth="['config:worker-group:update']"
              :content="$t('Edit')"
              placement="top"
            >
              <a
                href="javascript:;"
                @click="_edit(item)"
              >编辑</a>
            </el-tooltip>
            <el-popconfirm
              v-auth="['config:worker-group:delete']"
              :confirmButtonText="$t('Confirm')"
              :cancelButtonText="$t('Cancel')"
              icon="el-icon-info"
              iconColor="red"
              :title="item.central === 1 ? '当前分组已设置为中心节点，请谨慎删除。':$t('Delete?')"
              @onConfirm="_delete(item,item.id)"
            >
              <a
                slot="reference"
                href="javascript:;"
              >删除</a>
            </el-popconfirm>
          </div>
        </div>
        <div class="m-card__body">
          <div class="res-list">
            <div class="res-item">
              <span>worker数量</span>
              <span>{{ item.addrList.split(',').length }}</span>
            </div>
            <div class="res-item">
              <span>CPU使用率</span>
              <span>{{ parseInt(item.cpuUsage*100) }}%</span>
            </div>
            <div class="res-item">
              <span>内存使用率</span>
              <span>{{ parseInt(item.memoryUsage * 100) }}%</span>
            </div>
          </div>
          <!-- <div class="number-box-wrap">
                        <div class="number-box">
                            <div class="number-top">
                                <span class="name">总CPU算力</span>
                                <span class="unit">核数</span>
                            </div>
                            <div class="number-val">0</div>
                        </div>
                        <div class="number-box">
                            <div class="number-top">
                                <span class="name">总GPU算力</span>
                                <span class="unit">ops</span>
                            </div>
                            <div class="number-val">0</div>
                        </div>
                    </div> -->
          <div class="res-list">
            <div class="res-item">
              <span>创建时间</span>
              <span>{{item.createTime | formatDate}}</span>
            </div>
            <div class="res-item">
              <span>更新时间</span>
              <span v-if="item.name !=='default'">{{item.updateTime | formatDate}}</span>
              <span v-else>-</span>
            </div>
            <div
              class="res-item"
              style="margin-top: 10px;"
            >
              <span><router-link :to="{path:'/scheduler/resource',query:{groupId:item.id}}">查看详情</router-link></span>
            </div>
          </div>
        </div>
      </div>
    </el-col>
  </el-row>
  <!-- <div class="list-model">
    <div class="table-box">
      <el-table :data="list" size="mini" style="width: 100%">
        <el-table-column type="index" :label="$t('#')" width="50"></el-table-column>
        <el-table-column prop="name" :label="$t('Group')"></el-table-column>
        <el-table-column :label="$t('Addresses')" min-width="300">
          <template slot-scope="scope">
            <span style="display: inline-block; margin-right: 10px">{{scope.row.addrList}}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('Create Time')" min-width="120">
          <template slot-scope="scope">
            <span>{{scope.row.createTime | formatDate}}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('Update Time')" min-width="120">
          <template slot-scope="scope">
            <span>{{scope.row.updateTime | formatDate}}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('Operation')" width="100">
          <template slot-scope="scope">
            <el-tooltip :content="$t('Edit')" placement="top" v-if="!scope.row.systemDefault">
              <el-button type="primary" size="mini" icon="el-icon-edit-outline" @click="_edit(scope.row)" circle></el-button>
            </el-tooltip>
            <el-tooltip :content="$t('Delete')" placement="top" v-if="!scope.row.systemDefault">
              <el-popconfirm
                :confirmButtonText="$t('Confirm')"
                :cancelButtonText="$t('Cancel')"
                icon="el-icon-info"
                iconColor="red"
                :title="$t('Delete?')"
                @onConfirm="_delete(scope.row,scope.row.id)"
              >
                <el-button type="danger" size="mini" icon="el-icon-delete" circle slot="reference"></el-button>
              </el-popconfirm>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div> -->
</template>
<script>
  import { mapActions } from 'vuex'

  export default {
    name: 'user-list',
    data () {
      return {
        list: []
      }
    },
    props: {
      workerGroupList: Array,
      pageNo: Number,
      pageSize: Number
    },
    methods: {
      ...mapActions('security', ['deleteWorkerGroups']),
      _delete (item, i) {
        this.deleteWorkerGroups({
          id: item.id
        }).then(res => {
          this.$emit('on-update')
          this.$message.success(res.msg)
        }).catch(e => {
          this.$message.error(e.msg || '')
        })
      },
      _edit (item) {
        this.$emit('on-edit', item)
      }
    },
    watch: {
      workerGroupList (a) {
        this.list = []
        setTimeout(() => {
          this.list = a
        })
      }
    },
    created () {
      this.list = this.workerGroupList
    },
    mounted () { },
    components: {}
  }
</script>
<style scoped lang="scss">
.m-card {
  background: #ffffff;
  border: 1px solid rgba(51, 49, 126, 0.2);
  border-radius: 8px;
  margin-top: 20px;
  &.is-home {
    background: #e7e5ff;
  }
  &.is-center {
    background: linear-gradient(180deg, rgba(51, 49, 126, 0.8), rgba(69, 17, 109, 0.8) 100%);
    border: 1px solid #33317e;
    border-radius: 4px;
    box-shadow: 0px 0px 14px 0px rgba(86, 81, 254, 0.7);
    backdrop-filter: blur(2px);
    color: #ffffff;
    a {
      color: #ffffff !important;
    }
  }
  &__header {
    padding: 15px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid rgba(51, 49, 126, 0.1);
    font-size: 16px;
    .btn-group {
      font-size: 14px;
      a {
        margin: 0 5px;
      }
    }
  }
  &__body {
    padding: 20px;
  }
}

.res-item {
  display: flex;
  justify-content: space-between;
  line-height: 22px;
  a {
    text-decoration: underline;
  }
}

.number-box-wrap {
  display: flex;
  margin: 20px -5px;
  color: #333333;
  .number-box {
    flex: 1;
    margin: 0 5px;
    padding: 10px;
    background: #f5f6f8;
    border-radius: 8px;
    .number-val {
      font-size: 40px;
      font-family: Oxanium, Oxanium-Bold;
      font-weight: 700;
      margin: 10px auto;
      text-align: center;
    }
  }
  .number-top {
    display: flex;
    justify-content: space-between;
    .unit {
      color: #999999;
    }
  }
}
</style>
